#include "sfx.h"

#include "part2d/part2d.h"
#include "num/rand.h"

#include "util/timer.h"				// log
#include "util/log.h"				// log
#include "util/common_macros.h"		// macros
#include "util/mmgr.h"		    

using namespace vfx;

EffectMan::EffectMan( )
{

}

EffectMan::~EffectMan()
{
	for (unsigned int i=0; i<m_Set.size(); i++)
	{
		delete m_Set[i];
	}
}

void	EffectMan::DropSet(SFXSet* pSet, bool Delete)
{
	for (unsigned int i=0; i<m_Set.size(); i++)
	{
		if (m_Set[i] == pSet)
		{	
			m_Set.erase(m_Set.begin() + i);
			if (Delete) delete pSet;
		}		
	}
}

void EffectMan::Tick(float dt)
{
	for (unsigned int i=0; i<m_Set.size(); i++)
	{
		m_Set[i]->Tick(dt);
	}
}

void EffectMan::Render(const math::Vec2& Offset, float Scale)
{
	m_Offset = Offset;
	m_Scale = Scale;
	for (unsigned int i=0; i<m_Set.size(); i++)
	{
		m_Set[i]->Render(Offset, m_Scale);
	}
}

//--------------------------------------------------------

Part2DSFXSet::Part2DSFXSet(EffectMan*		pParent) : 
	SFXSet(pParent),
	m_pSys(NULL)
{


}

Part2DSFXSet::~Part2DSFXSet()
{
	UTIL_SAFE_DELETE(m_pSys);

}
void	Part2DSFXSet::Render(const math::Vec2& Offset, float Scale)
{
	if (m_pSys)
	{
		m_pSys->Render(Offset, Scale);
	}
}
void	Part2DSFXSet::Tick(float dt)
{
	if (m_pSys)
	{
		m_pSys->Tick(dt);
	}
}
void	Part2DSFXSet::Load(const std::string& File)
{
	UTIL_SAFE_DELETE(m_pSys);
	m_pSys = new part2d::Part2D();
	m_pSys->Load(File);
}
void	Part2DSFXSet::TriggerParticles(int Count, const math::Vec2& Pos, const math::Vec2& Size)
{
	if (m_pSys)
	{
	   num::RAND32& Rnd = num::RAND32::Instance();
	   for (int i=0; i<Count; i++)
	   {
		   m_pSys->TriggerParticles(Pos + Rnd.RandVec2(math::Vec2(0,0), Size));
	   }
	}
}

void	Part2DSFXSet::TriggerParticles(int Count, const math::Vec2& Pos)
{
	if (m_pSys)
	{
	   for (int i=0; i<Count; i++)
	   {
		   m_pSys->TriggerParticles(Pos);
	   }
	}
}
